package com.zhy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.example.jl.pickpiclibrary.adapter.BigPhotoViewPagerAdaper;
import com.example.jl.pickpiclibrary.adapter.GridAdapter;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.ArrayList;


public class MainActivityActivity extends Activity {
    private static final int REQUEST_PICK = 0;
    private static final int SHOW_BIG = 1;
    private GridView gridview;
    private GridAdapter adapter;
    private ArrayList<String> selectedPicture = new ArrayList<String>();
    private ArrayList<String> moreselectedPicture = new ArrayList<String>();
    private ArrayList<String> revisedPicture = new ArrayList<String>();
    private float width;
    private float height;


    private ViewPager view_pager;
    private BigPhotoViewPagerAdaper bigPhotoViewPagerAdaper;
    private ImageLoader imageLoader;
    private ArrayList<View> views;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        gridview = (GridView) findViewById(R.id.gridview);
        //大图展示
        view_pager = (ViewPager)findViewById(com.example.jl.pickpiclibrary.R.id.view_pager);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator()).diskCacheSize(100 * 1024 * 1024)
                .diskCacheFileCount(300).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);



        //获取屏幕宽高
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        height = metric.heightPixels;
        width = metric.widthPixels;


        adapter = new GridAdapter(this, selectedPicture);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                float x = location[0] + view.getWidth() / 2;
                float y = location[1] + view.getHeight() / 2;
                Animation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f,
                        Animation.RELATIVE_TO_PARENT, x / width,
                        Animation.RELATIVE_TO_PARENT, y / height);
                scaleAnimation.setDuration(2000);
                scaleAnimation.setFillAfter(true);
                scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        view_pager.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                view_pager.setAdapter(bigPhotoViewPagerAdaper);
                view_pager.setCurrentItem(position);
                view_pager.startAnimation(scaleAnimation);
            }
        });
    }

    private ArrayList<View> initListViews( ArrayList<String>  imagepaths) {
        ArrayList<View> Views = new ArrayList<>();
        for (String imagepath:imagepaths){
            ImageView imageView = new ImageView(this);
            imageLoader.displayImage("file://" + imagepath, imageView);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            Views.add(imageView);
        }
        return Views;
    }
    public void selectPicture(View view) {
        startActivityForResult(new Intent(this, com.example.jl.pickpiclibrary.activity.PickPicActivity.class), REQUEST_PICK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        switch (requestCode) {
            case REQUEST_PICK:
                if (resultCode == RESULT_OK) {
                    moreselectedPicture = (ArrayList<String>) data.getSerializableExtra(com.example.jl.pickpiclibrary.activity.PickPicActivity.INTENT_SELECTED_PICTURE);
                    selectedPicture.addAll(moreselectedPicture);
                    views =initListViews(selectedPicture);
                    bigPhotoViewPagerAdaper = new BigPhotoViewPagerAdaper(views);
                    adapter.notifyDataSetChanged();
                }
                break;
            case SHOW_BIG:
                if (resultCode == RESULT_OK) {
                    selectedPicture.clear();
                    revisedPicture = (ArrayList<String>) data.getSerializableExtra("revisedPicture");
                    selectedPicture.addAll(revisedPicture);
                    views =initListViews(selectedPicture);
                    bigPhotoViewPagerAdaper = new BigPhotoViewPagerAdaper(views);
                    adapter.notifyDataSetChanged();
                }
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            moreselectedPicture.clear();
            revisedPicture.clear();
            selectedPicture.clear();
            adapter.notifyDataSetChanged();
            Animation scaleAnimation = new ScaleAnimation(1f, 0f, 1f, 0f);
            scaleAnimation.setDuration(1000);
            scaleAnimation.setFillAfter(true);
            scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    view_pager.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            view_pager.startAnimation(scaleAnimation);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
